package com.twotiger.and.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.activity.account.cash.CashBankCardList;
import com.twotiger.and.bean.BanklistItem;
import com.twotiger.and.bean.CashBank;
import com.twotiger.and.util.DownPicUtil;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.StringUtils;

public class BankcardAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private TextView bank_detail_text_item;
    private TextView bankcard_no_item;
    private ImageView bankcard_icon_item;
    private CheckBox bank_check_btn;
    /**当前选中*/
    private View selectedView;

    private List<CashBank> bankcardlist;
    private CashBankCardList activity;

    public BankcardAdapter(CashBankCardList context, List<CashBank> bankcardlist) {
        super();
        this.activity = context;
        inflater = LayoutInflater.from(context);
        this.bankcardlist = bankcardlist;
    }

    @Override
    public int getCount() {
        return bankcardlist.size();
    }
    
    public void resetBankCardList(List<CashBank> newbankcardlist){
        bankcardlist.clear();
        if (!ListUtils.isEmpty(newbankcardlist)) {
            bankcardlist.addAll(newbankcardlist);
        }
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.choice_bankcard_item, null);
        bankcard_icon_item = (ImageView) view.findViewById(R.id.bankcard_icon_item);
        bankcard_no_item = (TextView) view.findViewById(R.id.bankcard_no_item);
        bank_detail_text_item = (TextView) view.findViewById(R.id.bank_detail_text_item);
        bank_check_btn = (CheckBox) view.findViewById(R.id.bank_check_btn);
        bank_check_btn.setEnabled(false);
        bank_check_btn.setChecked(false);
        //初始化数据
        CashBank item = bankcardlist.get(position);
        if (item.isCashChecked) {
            bank_check_btn.setChecked(true);
            selectedView = view;
        }

        bankcard_no_item.setText(StringUtils.hidecardMidString(item.cardNo));
        bank_detail_text_item.setText(item.name);
        
        //下载logo并放到imageview上
        new downBankLogoTask(bankcard_icon_item).execute(Constants.HOST + item.getBankLogo());

        //添加监听事件
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox tmp = (CheckBox) v.findViewById(R.id.bank_check_btn);
                tmp.setChecked(true);
                if (selectedView != null) {
                    tmp = (CheckBox) selectedView.findViewById(R.id.bank_check_btn);
                    tmp.setChecked(false);
                }
                selectedView = v;
                Intent data = new Intent().putExtra("idx", position);
                activity.setResult(activity.RESULT_OK, data);
                activity.finish();
                activity.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
            }
        });
        return view;
    }
    
    class downBankLogoTask extends AsyncTask<String, Integer, Boolean> {

        private Bitmap bankLogo;
        private ImageView mIv;
        
        public downBankLogoTask(ImageView iv){
            this.mIv = iv;
        }

        @Override
        protected Boolean doInBackground(String... params) {
         bankLogo = DownPicUtil.getNetWorkBitmap(params[0]);
         return true;
        }

        // 下载完成回调
        @Override
        protected void onPostExecute(Boolean result) {
            mIv.setBackgroundDrawable(new BitmapDrawable(bankLogo));
            super.onPostExecute(result);
        }

        // 更新进度回调
        @Override
        protected void onProgressUpdate(Integer... values) {
         super.onProgressUpdate(values);
        }

       }

}
